<?php

require_once dirname(dirname(__FILE__)) . '/config/config.php';
require_once DOCUMENT_ROOT . DIR_SHARED_CONFIG . 'connection.php';


class dao_user {

    //Methods
    public function __construct() {

    }

    public function update_last_login($id, $now) {
        $db = new connection();
        $con = $db->open_connect();

        $query = "UPDATE tbl_user SET "
                . "last_login = '" . $now . "' "
                . "WHERE id = " . $id;

        mysqli_query($con, $query) or die("Query fail: " . mysqli_error());

        $db->close_connect();
        return TRUE;
    }

    public function update_balance($id, $balance) {
        $db = new connection();
        $con = $db->open_connect();

        $query = "UPDATE tbl_user SET "
                . "balance = " . $balance . " "
                . "WHERE id = " . $id;

        mysqli_query($con, $query) or die("Query fail: " . mysqli_error());

        $db->close_connect();
        return TRUE;
    }

    public function get_all() {
        $db = new connection();
        $con = $db->open_connect();

        $query = "SELECT * FROM tbl_user WHERE role_id = 2";
        $result = mysqli_query($con, $query)
        or die("Query fail: " . mysqli_error());

        $list = array();
        while ($row = mysqli_fetch_array($result)) {
            array_push($list, $row);
        }
        $db->close_connect();
        return $list;
    }

    public function active($id) {
        $db = new connection();
        $con = $db->open_connect();
        $query = "UPDATE tbl_user SET "
                . "status_id = 2 "
                . "WHERE id = " . $id;
        mysqli_query($con, $query) or die("Query fail: " . mysqli_error());

        $db->close_connect();
        return TRUE;
    }

    public function register_normal($first_name,
                $last_name, $phone_number, $email, $username, $password, $activate_code, $invite_by_user_id) {
        $db = new connection();
        $con = $db->open_connect();

        $query = "INSERT INTO tbl_user(email, username, password, role_id,
            first_name, last_name, phone_number, balance, profit, status_id, activate_code, invite_by_user_id) VALUES (
            '" . $email . "','" . $username . "','" . md5($password) . "'," . 2 . ",'" . $first_name . "','" . $last_name . "','" . $phone_number . "'"
                . "," . 0 . "," . 0 . "," . 1 . ",'" . $activate_code . "', $invite_by_user_id)";
        mysqli_query($con, $query) or die("Query fail: " . mysqli_error());
        $inserted_id = mysqli_insert_id($db->con);
        $db->close_connect();
        return $inserted_id;
    }

    public function update_affiliate_profit($id, $affiliate_profit) {
        $db = new connection();
        $con = $db->open_connect();

        $query = "UPDATE tbl_user SET "
                . "affiliate_profit = '" . $affiliate_profit . "' "
                . "WHERE id = " . $id;
        mysqli_query($con, $query) or die("Query fail: " . mysqli_error());

        $db->close_connect();
        return TRUE;
    }
    
    public function update_affiliate_commission($id, $affiliate_commission) {
        $db = new connection();
        $con = $db->open_connect();

        $query = "UPDATE tbl_user SET "
                . "affiliate_commission = '".$affiliate_commission."' "
                . "WHERE id = " . $id;
        mysqli_query($con, $query) or die("Query fail: " . mysqli_error());

        $db->close_connect();
        return TRUE;
    }
    
    public function update_is_affiliate($id) {
        $db = new connection();
        $con = $db->open_connect();

        $query = "UPDATE tbl_user SET "
                . "is_affiliate = 1 "
                . "WHERE id = " . $id;
        mysqli_query($con, $query) or die("Query fail: " . mysqli_error());

        $db->close_connect();
        return TRUE;
    }
    
    public function save($user_id, $first_name,
                $last_name, $phone_number, $email, $password, $role_id, $balance, $profit, $status_id, $invite_by_user_id, $last_login) {
        $db = new connection();
        $con = $db->open_connect();

        if ($user_id == 0) {
            $query = "INSERT INTO tbl_user(email, password, role_id,
                first_name, last_name, phone_number, balance, profit, status_id, invite_by_user_id, last_login) VALUES (
                '" . $email . "','" . md5($password) . "'," . $role_id . ",'" . $first_name . "','" . $last_name . "','" . $phone_number . "'"
                    . "," . $balance . "," . $profit . "," . $status_id . "," . $invite_by_user_id . ",'" . $last_login . "')";
        } else {
            $query = "UPDATE tbl_user SET "
                    . "email = '" . $email . "',"
                    . "password = '" . $password . "',"
                    . "role_id = " . $role_id . ","
                    . "first_name = '" . $first_name . "',"
                    . "last_name = '" . $last_name . "',"
                    . "phone_number = '" . $phone_number . "',"

                    . "balance = " . $balance . ","
                    . "profit = " . $profit . ","
                    . "status_id = " . $status_id . ","
                    . "invite_by_user_id = " . $invite_by_user_id . ","
                    . "last_login = '" . $last_login . "' "
                    . "WHERE id = " . $user_id;
        }
        mysqli_query($con, $query) or die("Query fail: " . mysqli_error());

        $db->close_connect();
        return TRUE;
    }

    public function get_by_id($id) {
        $db = new connection();
        $con = $db->open_connect();

        $query = "SELECT * FROM tbl_user WHERE id = " . $id;
        $result = mysqli_query($con, $query) or die("Query fail: " . mysqli_error());

        $row = mysqli_fetch_array($result);

        $db->close_connect();
        return $row;
    }

    public function get_by_id_without_role($id) {
        $db = new connection();
        $con = $db->open_connect();

        $query = "SELECT * FROM tbl_user WHERE id = " . $id;
        $result = mysqli_query($con, $query) or die("Query fail: " . mysqli_error());

        $row = mysqli_fetch_array($result);

        $db->close_connect();
        return $row;
    }

    public function get_by_activate_code($username, $activate_code) {
        $db = new connection();
        $con = $db->open_connect();

        $query = "SELECT * FROM tbl_user "
                . "WHERE role_id = 2 "
                . "AND username = '" . $username . "' "
                . "AND activate_code = '" . $activate_code . "' ";
        $result = mysqli_query($con, $query) or die("Query fail: " . mysqli_error());

        $row = mysqli_fetch_array($result);

        $db->close_connect();
        return $row;
    }

    public function delete($id) {
        $db = new connection();
        $con = $db->open_connect();

        $query = "DELETE FROM tbl_user WHERE id = " . $id;
        mysqli_query($con, $query) or die("Query fail: " . mysqli_error());

        $db->close_connect();
        return TRUE;
    }

    public function get_by_login_info($username, $password) {
        $db = new connection();
        $con = $db->open_connect();

        $query = "SELECT u.* FROM tbl_user u LEFT JOIN tbl_role r ON u.role_id = r.id "
                . "WHERE username = '" . $username . "' "
                . "AND password = '" . md5($password) . "' "
                . "AND status_id = 2 ";
        $result = mysqli_query($con, $query);
        if (!$result) {
            printf("Error: %s\n", mysqli_error($con));
            exit();
        }

        return mysqli_fetch_array($result);
    }

    public function get_by_username($username) {
        $db = new connection();
        $con = $db->open_connect();

        $query = "SELECT u.* FROM tbl_user u JOIN tbl_role r ON u.role_id = r.id "
                . "WHERE u.username = '" . $username . "'";
        $result = mysqli_query($con, $query);
        if (!$result) {
            printf("Error: %s\n", mysqli_error($con));
            exit();
        }

        return mysqli_fetch_array($result);
    }

    public function get_by_email($email) {
        $db = new connection();
        $con = $db->open_connect();

        $query = "SELECT u.* FROM tbl_user u JOIN tbl_role r ON u.role_id = r.id "
                . "WHERE u.email = '" . $email . "'";
        $result = mysqli_query($con, $query);
        if (!$result) {
            printf("Error: %s\n", mysqli_error($con));
            exit();
        }

        return mysqli_fetch_array($result);
    }

    public function get_first() {
        $db = new connection();
        $con = $db->open_connect();

        $query = "SELECT * FROM tbl_user "
                . "WHERE role_id = 2 "
                . "LIMIT 0, 1";
        $result = mysqli_query($con, $query) or die("Query fail: " . mysqli_error());

        $row = mysqli_fetch_array($result);

        $db->close_connect();
        return $row;
    }

}
